public class Test {
    /*
    题目 1： 第 n 个泰波那契数
     */
    // 时间复杂度 O(n)
    // 空间复杂度 O(n)
    public int tribonacci1(int n) {
        // 动态规划的常用步骤
        // 1. 创建 dp 表
        // 2. 初始化
        // 3. 填满 dp 表
        // 4. 返回值

        // 处理一下边界值
        if(n == 0) return 0;
        if(n == 1 || n == 2) return 1;

        int[] dp = new int[n + 1];
        dp[0] = 0;
        dp[1] = 1;
        dp[2] = 1;

        for(int i = 3; i <= n; i++){
            dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3];
        }

        return dp[n];
    }

    // 进行空间优化
    // 使用滚动数组，使空间复杂度降为  O(1)
    public int tribonacci(int n) {
        if(n == 0) return 0;
        if(n == 1 || n == 2) return 1;

        int a = 0;
        int b = 1;
        int c = 1;
        int d = 0;

        for(int i = 3; i <= n; i++){
            d = a + b + c;
            a = b;
            b = c;
            c = d;
        }

        return d;
    }

    /*
    题目 2：三步问题
     */
    public int waysToStep(int n) {
        int MOD = (int)1e9 + 7;
        // 边界
        if(n == 1 || n == 2) return n;
        if(n == 3) return 4;

        int[] dp = new int[n + 1];
        dp[1] = 1;
        dp[2] = 2;
        dp[3] = 4;

        for(int i = 4; i <= n; i++){
            dp[i] = ((dp[i - 1] + dp[i - 2]) % MOD + dp[i - 3]) % MOD;
        }

        return dp[n];
    }

    /*
    题目 3：最小花费爬楼梯
     */
    public int minCostClimbingStairs1(int[] cost) {
        int n = cost.length;
        if(n == 1) return cost[0];
        if(n == 2) return Math.min(cost[0],cost[1]);

        int[] dp = new int[n + 1];
        dp[0] = dp[1] = 0;

        for(int i = 2; i <= n; i++){
            dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
        }

        return dp[n];
    }

    // 解法一：以 i 位置为结尾
    // dp[i] 表示 到达 i 台阶的最小花费
    public int minCostClimbingStairs(int[] cost) {
        int n = cost.length;

        int[] dp = new int[n + 1];
        // dp[0] = dp[1] = 0; 不初始化也为 0

        for(int i = 2; i <= n; i++){
            dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
        }

        return dp[n];
    }

    // 解法二： 以 i 位置为起点：
    // 从 i 位置出发，到达楼顶，此时的最小花费！
    public int minCostClimbingStairs2(int[] cost) {
        int n = cost.length;

        int[] dp = new int[n];

        dp[n - 1] = cost[n - 1];
        dp[n - 2] = cost[n - 2];

        for(int i = n - 3; i >= 0; i--){
            dp[i] = Math.min(dp[i + 1], dp[i + 2]) + cost[i];
        }

        return Math.min(dp[0], dp[1]);
    }

    /*
    题目 4：解码方法
     */
    public int numDecodings1(String ss) {
        // 1. 创建 dp 表
        // 2. 初始化
        // 3. 填表
        // 4. 返回值
        int n = ss.length();
        char[] s = ss.toCharArray();

        int[] dp = new int[n];
        if(s[0] != '0'){
            dp[0] = 1;
        }
        if(n == 1){
            return dp[0];
        }

        if(s[0] != '0' && s[1] != '0'){
            dp[1] += 1;
        }

        int t = 10 * (s[0] - '0') + s[1] - '0';
        if(10 <= t && t <= 26){
            dp[1] += 1;
        }

        for(int i = 2; i < n; i++){
            if(s[i] != '0'){
                dp[i] += dp[i - 1];
            }

            int tt = 10 * (s[i - 1] - '0') + s[i] - '0';

            if(10 <= tt && tt <= 26){
                dp[i] += dp[i - 2];
            }
        }

        return dp[n - 1];
    }

    // 对 初始化 这一步进行化简，dp 表创建一个辅助点
    public int numDecodings(String ss) {
        int n = ss.length();
        char[] s = ss.toCharArray();

        int[] dp = new int[n + 1];
        dp[0] = 1;

        if(s[0] != '0'){
            dp[1] = 1;
        }

        for(int i = 2; i <= n; i++){
            if(s[i - 1] != '0') {
                dp[i] += dp[i - 1];
            }

            int t = 10 * (s[i - 2] - '0') + s[i - 1] - '0';
            if(10 <= t && t <= 26){
                dp[i] += dp[i - 2];
            }
        }
        return dp[n];
    }

    /*
    题目 5：不同路径
     */
    public int uniquePaths(int m, int n) {
        int[][] dp = new int[m + 1][n + 1];

        dp[0][1] = 1;

        for(int i = 1; i < m + 1; i++){
            for(int j = 1; j < n + 1; j++){
                dp[i][j] = dp[i][j - 1] + dp[i - 1][j];
            }
        }

        return dp[m][n];
    }

    /*
    题目 6：不同路径Ⅱ
     */
    public static int uniquePathsWithObstacles1(int[][] obstacleGrid) {
        int m = obstacleGrid.length;
        int n = obstacleGrid[0].length;

        int[][] dp = new int[m + 1][n + 1];

        dp[0][1] = -1;

        for(int i = 1; i <= m; i++){
            for(int j = 1; j <= n; j++){
                dp[i][j] = obstacleGrid[i - 1][j - 1];
                if(dp[i][j] == 1){
                    continue;
                }
                if(dp[i - 1][j] != 1 && dp[i][j - 1] != 1){
                    dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
                }else if(dp[i - 1][j] != 1){
                    dp[i][j] = dp[i - 1][j];
                }else if(dp[i][j - 1] != 1){
                    dp[i][j] =  dp[i][j - 1];
                }else{
                    dp[i][j] = 0;
                }
            }
        }
        int ret = dp[m][n];
        return ret == 1 ? 0 : ret * -1;
    }

    public static void main1(String[] args) {
        int[][] arr = {{0,0},{0,1}};
        int ret = uniquePathsWithObstacles1(arr);
        System.out.println(ret);
    }

    public int uniquePathsWithObstacles2(int[][] obstacleGrid) {
        int m = obstacleGrid.length;
        int n = obstacleGrid[0].length;

        int[][] dp = new int[m + 1][n + 1];

        dp[0][1] = 1;

        for(int i = 1; i <= m; i++){
            for(int j = 1; j <= n; j++){
                if(obstacleGrid[i - 1][j - 1] == 1){
                    dp[i][j] = 0;
                    continue;
                }

                dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
            }
        }
        return dp[m][n];
    }

    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int m = obstacleGrid.length;
        int n = obstacleGrid[0].length;

        int[][] dp = new int[m + 1][n + 1];

        dp[0][1] = 1;

        for(int i = 1; i <= m; i++){
            for(int j = 1; j <= n; j++){
                if(obstacleGrid[i - 1][j - 1] == 0){
                    dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
                }
            }
        }
        return dp[m][n];
    }

    /*
    题目 7：礼物的最大价值
     */
    public int maxValue(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;

        int[][] dp = new int[m + 1][n + 1];
        for(int i = 1; i <= m; i++){
            for(int j = 1; j <= n; j++){
                dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]) + grid[i - 1][j - 1];
            }
        }

        return dp[m][n];
    }

    /*
    题目 8：下降路径的最小和
     */
    public int minFallingPathSum1(int[][] matrix) {
        int m = matrix.length;
        int n = matrix[0].length;

        int[][] dp = new int[m + 1][n + 2];
        for(int i = 0; i < m + 1; i++){
            for(int j = 0; j < n + 2; j++){
                dp[i][j] = Integer.MAX_VALUE;
            }
        }

        for(int i = 0; i < n + 2; i++){
            dp[0][i] = 0;
        }

        for(int i = 1; i < m + 1; i++){
            for(int j = 1; j < n + 1; j++){
                int a = Math.min(dp[i - 1][j - 1], dp[i - 1][j]);
                int b = Math.min(a, dp[i -1][j + 1]);
                dp[i][j] = b + matrix[i - 1][j - 1];
            }
        }

        int min = Integer.MAX_VALUE;
        for(int i = 1; i < n + 1; i++){
            if(min > dp[m][i]){
                min = dp[m][i];
            }
        }
        return min;
    }

    public int minFallingPathSum(int[][] matrix) {
        int n = matrix.length;

        int[][] dp = new int[n + 1][n + 2];

        for(int i = 1; i < n + 1; i++){
            dp[i][0] = dp[i][n + 1] = Integer.MAX_VALUE;
        }

        for(int i = 1; i < n + 1; i++){
            for(int j = 1; j < n + 1; j++){
                dp[i][j] = Math.min(dp[i - 1][j - 1], Math.min(dp[i - 1][j], dp[i - 1][j + 1])) + matrix[i - 1][j - 1];
            }
        }

        int ret = Integer.MAX_VALUE;
        for(int i = 1; i < n + 1; i++){
            ret = Math.min(ret, dp[n][i]);
        }
        return ret;
    }

    /*
    题目 9：最小路径和
     */            
    public int minPathSum(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;

        int[][] dp = new int[m + 1][n + 1];

        for(int i = 2; i < n + 1; i++){
            dp[0][i] = Integer.MAX_VALUE;
        }

        for(int i = 2; i < m + 1; i++){
            dp[i][0] = Integer.MAX_VALUE;
        }

        for(int i = 1; i < m + 1; i++){
            for(int j = 1; j < n + 1; j++){
                dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + grid[i - 1][j - 1];
            }
        }

        return dp[m][n];
    }

    /*
    题目 10：地下城游戏
     */
    public int calculateMinimumHP(int[][] dungeon) {
        int m = dungeon.length;
        int n = dungeon[0].length;

        int[][] dp = new int[m + 1][n + 1];

        for(int i = 0; i <= n - 1; i++){
            dp[m][i] = Integer.MAX_VALUE;
        }

        for(int i = 0; i <= m - 1; i++){
            dp[i][n] = Integer.MAX_VALUE;
        }

        dp[m][n - 1] = 1;
        dp[m - 1][n] = 1;

        for(int i = m - 1; i >= 0; i--){
            for(int j = n - 1; j >= 0; j--){
                dp[i][j] = Math.min(dp[i + 1][j], dp[i][j + 1]) - dungeon[i][j];
                dp[i][j] = Math.max(dp[i][j] , 1);
            }
        }
        return dp[0][0];
    }
}
